<#assign ctx="${rc.contextPath}">
<html>
<head>
    <title>用户管理</title>
    <script type="text/javascript">
        $(document).ready(function () {
            var queryParams = {};
            var jsonSearchParam = $.cookie("quartz_search_param_header");
            if (jsonSearchParam) {
                queryParams = JSON.parse(jsonSearchParam);
                $("#frmItemSearch").form('load', queryParams);
            } else {
                queryParams = $("#frmItemSearch").serializeObject();
            }

            var winH = $(window).height();
            var toolH = $(".toolbar").outerHeight();
            var actionH = $(".action-area").outerHeight();
            var crumbH = $(".breadcrumb").outerHeight();
            var tgH = (winH - toolH - actionH - crumbH) * 0.7;

            $('#tg').datagrid({
                pagination: true,
                rownumbers: true,
                idField: 'name',
                url: '${ctx}/quartz/manage/list',
                height: tgH,
                onCheck: onCheck,
                onUncheck: onUncheck,
                onCheckAll: onCheckAll,
                onUncheckAll: onUncheckAll,
                singleSelect: false,
                selectOnCheck: true,
                checkOnSelect: true,
                queryParams: queryParams,
                onLoadSuccess: function () {
                    // 设置选中行
                    var dgArray = $('#tg').datagrid('getRows');
                    if (null != dgArray && undefined != dgArray && dgArray.length > 0) {
                        for (var drIndex = 0; drIndex < dgArray.length; drIndex++) {
                            var rows = window.sessionStorage.getItem(dgArray[drIndex].accUserId);
                            if (null != rows && undefined != rows) {
                                $('#tg').datagrid("selectRow", drIndex);
                            }
                        }
                    }
                }
            });
        })

        $(window).resize(function () {
            $("#tg").datagrid('resize');
        });

        // 当用户勾选一行时触发
        function onCheck(index, row) {
            // 将选中行保存到本地Session
            var id = row.accUserId;
            window.sessionStorage.setItem(id, JSON.stringify(row));
        }

        // 当用户取消勾选一行时触发
        function onUncheck(index, row) {
            var id = row.accUserId;
            if (undefined != window.sessionStorage.getItem(id) && null != window.sessionStorage.getItem(id)) {
                window.sessionStorage.removeItem(id);
            }
        }

        // 当用户勾选全部行时触发
        function onCheckAll(rows) {
            if (undefined != rows && null != rows && rows.length > 0) {
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    if (row) {// 将选中行保存到本地Session
                        var id = row.accUserId;
                        window.sessionStorage.setItem(id, JSON.stringify(row));
                    }
                }
            }
        }

        // 当用户取消勾选全部行时触发
        function onUncheckAll(rows) {
            if (undefined != rows && null != rows && rows.length > 0) {
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    if (row) {// 将选中行保存到本地Session
                        var id = row.accUserId;
                        if (undefined != window.sessionStorage.getItem(id) && null != window.sessionStorage.getItem(id))
                            window.sessionStorage.removeItem(id);
                    }
                }
            }
        }

        function addJob() {
            window.location.href = "${ctx}/quartz/manage/toSave";
        }

        function updateJob() {
            var rows = $("#tg").datagrid("getSelections");
            if (undefined == rows || null == rows || rows.length <= 0) {
                $.messager.alert('提示', "请选择要编辑的任务!", "warning");
                return;
            } else {
                if (undefined != rows && null != rows && rows.length > 1) {
                    $.messager.alert('提示', "不能同时修改多个任务!", "warning");
                    return;
                }
            }
            if (rows[0]) {
                var row = rows[0];
                var value = "jobName=" + row['jobName'] + "&jobGroupName=" + row['jobGroupName'] +
                        "&triggerName=" + row['triggerName'] + "&triggerGroupName=" + row['triggerGroupName'];
                window.location.href = "${ctx}/quartz/manage/toSave?" + value;
            }
        }

        function stopJob() {
            var rows = $("#tg").datagrid("getSelections");
            if (undefined == rows || null == rows || rows.length <= 0) {
                $.messager.alert('提示', "请选择要暂停的任务!", "warning");
                return;
            }else{
                $.messager.confirm('确认', '是否暂停勾选的任务?', function (r) {
                    if (r) {
                        $.ajax({
                            type: "POST",
                            url: '${ctx}/quartz/manage/stopJob',
                            data: {json: JSON.stringify(rows)},
                            dataType: 'json',
                            success: function (result) {
                                successMsg(result.message);
                                $('#tg').datagrid('reload');
                            }
                        });
                    }
                });
            }
        }

        function resumeJob() {
            var rows = $("#tg").datagrid("getSelections");
            if (undefined == rows || null == rows || rows.length <= 0) {
                $.messager.alert('提示', "请选择要恢复的任务!", "warning");
                return;
            } else {
                $.messager.confirm('确认', '是否恢复勾选的任务?', function (r) {
                    if (r) {
                        $.ajax({
                            type: "POST",
                            url: '${ctx}/quartz/manage/resumeJob',
                            data: {json: JSON.stringify(rows)},
                            dataType: 'json',
                            success: function (result) {
                                successMsg(result.message);
                                $('#tg').datagrid('reload');
                            }
                        });
                    }
                });
            }
        }

        function triggerJob() {
            var rows = $("#tg").datagrid("getSelections");
            if (undefined == rows || null == rows || rows.length <= 0) {
                $.messager.alert('提示', "请选择要立即执行的任务!", "warning");
                return;
            } else {
                $.messager.confirm('确认', '是否立即执行勾选的任务?', function (r) {
                    if (r) {
                        $.ajax({
                            type: "POST",
                            url: '${ctx}/quartz/manage/triggerJob',
                            data: {json: JSON.stringify(rows)},
                            dataType: 'json',
                            success: function (result) {
                                successMsg(result.message);
                                $('#tg').datagrid('reload');
                            }
                        });
                    }
                });
            }
        }

        function deleteJob() {
            var rows = $('#tg').datagrid('getSelections');
            if (rows.length > 0) {
                $.messager.confirm('确认', '是否删除勾选的任务?', function (r) {
                    if (r) {
                        $.ajax({
                            type: "POST",
                            url: '${ctx}/quartz/manage/deleteJob',
                            data: {json: JSON.stringify(rows)},
                            dataType: 'json',
                            success: function (result) {
                                successMsg(result.message);
                                $('#tg').datagrid('reload');
                                $('#tg').datagrid('clearSelections').datagrid('clearChecked');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', '请选择要删除的用户', "warning");
            }
        }

        function resetForm() {
            $("#frmItemSearch").resetForm();
            window.sessionStorage.clear();
        }

        $(document).keydown(function (e) {
            if (e.keyCode == 13) {
                search();
            }
        });

        function search() {
            var data = $("#frmItemSearch").serializeObject();
            $.cookie('quartz_search_param_header', JSON.stringify(data), {expires: 7});
            $('#tg').datagrid('load', data);
            $('#tg').datagrid('clearSelections').datagrid('clearChecked');
        }

        function departmentFormatter(value, row, index) {
            if (value == undefined) {
                return "";
            }
            return value.accDepartmentName;
        }
    </script>
</head>
<body>
<div class="breadcrumb"> 用户设置 > 用户管理</div>

<!-- 表单编辑区域 -->
<dl id="actEdit" class="action-area">
    <dt class="cf">
    <div class="form-name">搜索</div>
    <span class="arrow"></span>
    </dt>
    <dd>
        <form id="frmItemSearch" method="post">
            <table class="form-tb">
                <tr>
                    <th class="tb-lab">任务名</th>
                    <td>
                        <input class="easyui-validatebox text" name="triggerName" id="triggerName"
                               data-options="required:false"/>
                    </td>
                    <td>
                        <input class="btn btn-search" type="button" onclick="search()"/>
                        <input class="btn" type="button" onclick="resetForm();" value="重置条件"/>
                    </td>
                </tr>
            </table>
        </form>
        <div class="dg-wrap-expander dg-wrap-collapse"></div>
    </dd>
</dl>

<div id="toolbar" class="toolbar">
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-add" onclick="addJob();"
       plain="true">新增</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="updateJob();"
       plain="true">修改</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="stopJob();"
       plain="true">暂停</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="resumeJob();"
       plain="true">恢复</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="triggerJob();"
       plain="true">立即执行</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-delete" onclick="deleteJob();"
       plain="true">删除</a>
</div>

<table id="tg">
    <thead>
    <tr>
        <th data-options="field:'ck', width:20,checkbox:true "></th>
        <th data-options="field:'triggerName', width:100">任务名</th>
        <th data-options="field:'triggerGroupName', width:200">任务组名</th>
        <th data-options="field:'jobStatus', width:100">状态</th>
        <th data-options="field:'previousFireTime', width:150,formatter:dateTimeFormatter">上次运行时间</th>
        <th data-options="field:'nextFireTime', width:150,formatter:dateTimeFormatter">下次运行时间</th>
        <th data-options="field:'startTime', width:150,formatter:dateTimeFormatter">开始时间</th>
        <th data-options="field:'endTime', width:150,formatter:dateTimeFormatter">结束时间</th>
    </tr>
    </thead>
</table>

</body>
</html>